<script>
$(document).ready(function(){
	var loginyzm = '';
	var c = {
		web:'',
		loginsubmit:function(o){
			if(js.ajaxbool)return false;
			var user = '',pass = '';
			js.setmsg('');
			var ltype = get('logintype').value;

			if(ltype=='0'){
				user = get('adminuser').value,pass = get('adminpass').value;
				if(user==''){
					rockalert('用户名不能为空', function(){get('adminuser').focus()});
					return false;
				}
				if(pass==''){
					rockalert('密码不能为空', function(){get('adminpass').focus()});
					return false;
				}
			}else{
				user = get('adminmobile').value;
				if(user==''){
					js.msg('msg','手机号不能为空');
					get('adminmobile').focus();
					return false;
				}
				js.setoption('adminmobile', user);
				loginyzm = get('adminmobileyzm').value;
				if(loginyzm=='' || loginyzm.length!=6){
					js.msg('msg','手机验证码格式不对');
					get('adminmobileyzm').focus();
					return false;
				}
			}
			js.setoption('adminuser', user);
			js.setoption('adminpass', pass);
			var web = '';
			if(apicloud){
				web = ''+api.deviceName+''+api.deviceModel+'';
				web = strreplace(web.toLowerCase());
			}
			o.value='登录中...';
			o.disabled=true;
			xcy.initPushBool=false;
			var spd = {user:jm.base64encode(user),web:web,device:device,cfrom:CFROM,pass:jm.base64encode(pass),ltype:ltype,yanzm:loginyzm};
			loginyzm = '';
			js.ajax('login|check|none',spd, function(ret){
				o.value='登录成功';
				c.loginsuccess(ret.data);
			},'post',function(str,code, ret){
				o.value='登录';
				o.disabled=false;
				if(ret){
					var a = ret.data;
					if(a && a.shouji){
						mobilejsho = a.mobile;
						js.wx.prompt('输入手机验证码','手机号：'+a.shouji+'&nbsp;<span><a class="r-zhu" href="javascript:;" onclick="getcodes{rand}(this)">[获取验证码]</a></span>',function(txt){
							if(txt){
								loginyzm = txt;
								c.loginsubmit(o);
							}
						});;
					}
				}
			});
			return false;
		},
		init:function(){
			TOKEN = '';
			js.setoption(TOKENKEY,'');
			js.setoption('adminid','');
			var us=js.getoption('adminuser');
			if(us)$('#adminuser').val(us);
			$('#adminpass').val(js.getoption('adminpass'));
			$('#adminmobile').val(js.getoption('adminmobile'));
			var face = js.getoption('adminface');
			if(face)get('myface').src=face;
			if(logincogbool)$('#logincogbtn').show();
			$('#loginstring').html(js.getoption('loginfooter', loginstring));
			$('#footerdiv').hide();
			$('#homemenubtn').hide();
			this.changedys();
			this.yangzhen();
			us = js.getoption('title');
			if(us)$('#login_title').html(us);
			clearTimeout(xcy.homeretime);
		},
		loginsuccess:function(d){
			TOKEN = d.token;
			adminid = d.uid;
			js.setoption(TOKENKEY, TOKEN);
			js.setoption('adminface', d.face);
			js.setoption('adminname',d.name);
			js.setoption('adminzhan',d.user);
			js.setoption('deptallname',d.deptallname);
			js.setoption('ranking',d.ranking);
			js.setoption('adminid',d.uid);
			get('myface').src=d.face;
			$('#logincogbtn').hide();
			xcy.addtabs('','home');
		},
		setapiurl:function(dz){
			if(!dz)return;
			if(dz.substr(0,4)!='http'){
				this.bdyanz(dz);
				return;
			}
			if(dz.substr(-1)!='/')dz+='/';
			apiurl = dz;
			this.yangzhen('验证中...');
		},
		bdyanz:function(dz){
			var url = 'http://www.rockoa.com/';
			if(NOWURL.indexOf('http')>-1)url = 'http://'+HOST+'/app/rockxinhuweb/';
			var str = '验证中...';
			clearTimeout(this.yansxtttime);
			js.loading(str);
			$.getJSON(''+url+'api.php?a=oainfo&dz='+jm.base64encode(dz)+'&callback=?', function(ret){
				clearTimeout(c.yansxtttime);
				if(!ret.success){
					js.msg('msg', ret.msg);
					js.unloading();
				}else{
					apiurl = ret.data.url;
					c.yangzhen(str);
				}
			});
			this.yansxtttime=setTimeout(function(){js.unloading();js.wx.msgerror('无法验证');}, 3000);
		},
		yangzhen:function(str){
			js.ajax('login|appinit|none', false, function(ret){
				var da = ret.data;
				js.setoption('title', da.title);
				$('#login_title').html(da.title);
				js.setoption('apiurl', apiurl);
				js.setoption('logintype', da.loginyzm); //登录方式
				c.changedys();
			},'get', str);
		},
		changedys:function(){
			
			var lx = js.getoption('logintype');
			if(lx=='1' || lx=='2'){
				$('#yzmdengdiv').show();
			}else{
				$('#yzmdengdiv').hide();
			}
			if(lx=='3'){
				$('#loginview0').hide();
				$('#loginview1').show();
				get('logintype').value='1';
			}
		}
	}

	js.initbtn(c);
	c.init();
	
	var tempurlapi = '',nowshru='';
	setcogurl=function(){
		if(js.ajaxbool)return;
		if(apicloud){
			var das = ['确定此地址','取消'];
			if(tempurlapi==''){
				das.push('粘贴');
				tempurlapi = apiurl+'';
			}
			if(tempurlapi.indexOf('demo.rockoa.com')>-1)tempurlapi='';
			api.prompt({
				title:'系统地址',
				msg:'请输入你系统地址',
				text:tempurlapi,
				buttons: das
			}, function(ret, err) {
				var index = ret.buttonIndex;
				var dz = ret.text;
				tempurlapi='';
				if(index==1 && dz)c.setapiurl(dz);
				if(index==3)zhantieeff();
			});
		}else{
			tempurlapi = apiurl+'';
			if(tempurlapi.indexOf('demo.rockoa.com')>-1)tempurlapi='';
			js.wx.prompt('','请输入你系统地址,<a href="javascript:;" onclick="get(\'prompttxt\').value=\'\'" class="zhu">清空</a>',function(dz){
				c.setapiurl(dz);
			},tempurlapi);
		}
	}
	zhantieeff=function(){
		api.require('clipBoard').get(function(ret){
			if(ret.type=='url'){
				tempurlapi = ret.value;
				setcogurl();
			}
		});
	}

	getcodes{rand}=function(o1){
		var da = {'mobile':mobilejsho,'device':device};
		var o2 = $(o1).parent();
		o2.html(js.getmsg('获取中...'));
		js.ajax('yanzm|index',da, function(da){
			o2.html(js.getmsg('获取成功','green'));
		},'get',function(str, ret){
			o2.html(js.getmsg(str));
		});
	}
	getyzm{rand}=function(o1){
		mobilejsho = get('adminmobile').value;
		if(!mobilejsho){
			js.msg('msg','请输入手机号');
			get('adminmobile').focus();
			return;
		}
		var da = {'mobile':mobilejsho,'device':device};
		o1.value = '获取中...';
		js.setmsg();
		o1.disabled=true;
		js.ajax('yanzm|glogin',da, function(da){
			o1.value = '获取成功';
			js.msg('success', '验证码已发送到手机上');
			dshitime(60, o1);
		},'get',function(str, ret){
			o1.value = '重新获取';
			o1.disabled=false;
			js.setmsg(str);
		});

	}
	function dshitime(sj,o1){
		if(sj==0){
			o1.disabled=false;
			o1.value='重新获取';
			return;
		}
		o1.disabled=true;
		o1.value=''+sj+'';
		setTimeout(function(){dshitime(sj-1, o1)},1000);
	}

	changlogin=function(o1){
		if(js.ajaxbool)return;
		var ltype = get('logintype').value;
		if(ltype=='0'){
			$(o1).html('帐号密码登录');
			get('logintype').value='1';
			$('#loginview0').hide();
			$('#loginview1').show();
		}else{
			$(o1).html('验证码登录');
			get('logintype').value='0';
			$('#loginview1').hide();
			$('#loginview0').show();
		}
	}
});
</script>
<div style="padding:1em 0; text-align:center;margin-top:10px">
	<div><img id="myface" style="height:100px;width:100px;border-radius:50%" onclick="location.reload()" src="images/noface.png"></div>
</div>
<input type="hidden" id="logintype" value="0">
<div class="weui-cells weui-cells-form">
	<div id="loginview0">
		<div class="weui-cell">
			<div class="weui-cell__hd"><label style="width:70px" class="weui-label">用户名</label></div>
			<div class="weui_cell-bd weui-cell-primary">
				<input class="weui-input" onfocus="$('#loginfooter').hide()" onclick="this.focus()" type="text" value="" id="adminuser" onkeyup="if(event.keyCode==13)get('adminpass').focus()" maxlength="100" placeholder="用户名/姓名/手机号"/>
			</div>
		</div>

		<div class="weui-cell">
			<div class="weui-cell__hd"><label style="width:70px" class="weui-label">密码</label></div>
			<div class="weui-cell__bd weui-cell-primary">
				<input class="weui-input" onfocus="$('#loginfooter').hide()" onclick="this.focus()" id="adminpass" type="password" onkeyup="if(event.keyCode==13)get('loginbtn').click()"  placeholder="请输入密码"/>
			</div>
		</div>
	</div>
	<div id="loginview1" style="display:none">
		<div class="weui-cell">
			<div class="weui-cell__hd"><label style="width:70px" class="weui-label">手机号</label></div>
			<div class="weui-cell__bd weui-cell-primary">
				<input class="weui-input"  onfocus="$('#loginfooter').hide()" onclick="this.focus()" type="text" id="adminmobile"  onkeyup="if(event.keyCode==13)get('adminmobileyzm').focus()" maxlength="11" placeholder="请输入手机号"/>
			</div>
		</div>
		<div class="weui-cell">
			<div class="weui-cell__hd"><label style="width:70px" class="weui-label">验证码</label></div>
			<div class="weui-cell__bd weui-cell_primary">
				<input class="weui-input" onfocus="$('#loginfooter').hide()" onclick="this.focus()"  onkeyup="if(event.keyCode==13)get('loginbtn').click()" id="adminmobileyzm" maxlength="6" placeholder="请输入验证码"/>
			</div>
			<div class="weui-cell__ft">
                <input class="weui-btn weui-btn_mini weui-btn_primary" onclick="getyzm{rand}(this)" type="button"  value="获取">
            </div>
		</div>
	</div>
</div>

<div class="weui-btn-area" style="margin-top:24px">
	<input class="weui-btn weui-btn_primary" id="loginbtn" type="button" clickevt="loginsubmit" value="登录">
</div>

<div align="center" id="yzmdengdiv" style="padding:10px 0px;display:none"><a href="javascript:;" onclick="changlogin(this)" class="r-zhu">验证码登录</a></div>


<div id="loginfooter" style="display:none" class="weui-footer weui-footer_fixed-bottom">
	<p class="weui-footer__text" id="loginstring" style="color:#cccccc"></p>
</div>
